<?php
namespace TennisTournament\Controller;

use TennisTournament\Model\Group;
use TennisTournament\Model\StageOfTournamentEnum;
use TennisTournament\Service\GroupService;
use TennisTournament\Service\MatchService;
use Zend\Json\Json;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class GroupController extends AbstractActionController {

    /** @var GroupService */
    protected $groupService;

    /** @var MatchService */
    protected $matchService;

    /**
     * @param GroupService $groupService
     * @param MatchService $marchService
     */
    public function __construct(GroupService $groupService, MatchService $marchService) {
        $this->groupService = $groupService;
        $this->matchService = $marchService;
    }

    /**
     * Show all {@link TennisTournament\Model\Group} and statistics
     * inside them
     */
    public function showAllAction() {
        $collectionGroups = $this->groupService->getAllGroupsSorted();

        $collectionJSONGroups = array();

        for ($i = 0; $i < count($collectionGroups); $i++) {
            $collectionJSONGroups[$i] = $collectionGroups[$i]->toArray();
        }

        $jsonAllGroups = Json::encode(array('groups' => $collectionJSONGroups));

        $outputParameters = array('allGroups' => $jsonAllGroups);

        if ($this->matchService->isPlayedAllMatchesInStageOfTournament(StageOfTournamentEnum::GROUP_STAGE)) {
            $draws = array();

            $matches = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::QUARTERFINAL);
            for ($i = 0; $i < count($matches); $i++) {
                $matches[$i] = $matches[$i]->toArray();
            }
            $draws[StageOfTournamentEnum::QUARTERFINAL] = $matches;

            if ($this->matchService->isPlayedAllMatchesInStageOfTournament(StageOfTournamentEnum::QUARTERFINAL)) {

                $matches = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::SEMIFINAL);
                    for ($i = 0; $i < count($matches); $i++) {
                        $matches[$i] = $matches[$i]->toArray();
                    }
                    $draws[StageOfTournamentEnum::SEMIFINAL] = $matches;

                    if ($this->matchService->isPlayedAllMatchesInStageOfTournament(StageOfTournamentEnum::SEMIFINAL)) {
                        $matches = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::THIRD_PLACE);
                        for ($i = 0; $i < count($matches); $i++) {
                            $matches[$i] = $matches[$i]->toArray();
                        }
                        $draws[StageOfTournamentEnum::THIRD_PLACE] = $matches;

                        $matches = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::FINAL_STAGE);
                        for ($i = 0; $i < count($matches); $i++) {
                            $matches[$i] = $matches[$i]->toArray();
                        }
                        $draws[StageOfTournamentEnum::FINAL_STAGE] = $matches;
                    }
            }
            $outputParameters['draws'] = Json::encode($draws);
        }

        $viewModel = new ViewModel();
        $viewModel->setTemplate('tennis-tournament/group/show-all');

        return $viewModel->setVariables($outputParameters);
    }

    public function createAllAction() {
        $collectionGroupsJSON = Json::decode($_REQUEST['collectionGroupsJSON'], Json::TYPE_ARRAY);

        if ($this->groupService->isEligibleNumberOfGroups(count($collectionGroupsJSON))) {
            foreach($collectionGroupsJSON as $groupJSON) {
                $group = new Group($groupJSON);
                if (count($group->getStats()) >= GroupService::MIN_PLAYER_IN_GROUP) {
                    $this->groupService->save($group);
                    $this->matchService->createMatchesForSpecificGroup($group);
                }
            }
        }

        return $this->redirect()->toRoute('home');
    }

    
}